package com.gomore.opple.module.redpackage.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gomore.opple.R;
import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.module.IntentStart;
import com.gomore.opple.rest.redPackage.RedPackageGroupbyOrderNumber;
import com.gomore.opple.utils.BigDecimalUtils;
import com.gomore.opple.utils.DateUtil;
import com.gomore.opple.utils.DensityUtil;
import com.gomore.opple.widgets.adapter.CommonAdapter;
import com.gomore.opple.widgets.adapter.ViewHolder;

import java.util.List;

/**
 * Created by asus on 2017/5/26.
 */
public class RedPackageAdapter extends CommonAdapter<RedPackageGroupbyOrderNumber> {
    Activity mactivity;
    Context mContext;
    String mState;
    RedPackageGroupbyOrderNumber mredPackageGroupbyOrderNumber;
    int width, height;
    int recycler_width;

    public RedPackageAdapter(Activity activity, Context context, int layoutId, List<RedPackageGroupbyOrderNumber> redPackageGroupbyOrderNumberList, String state) {
        super(context, layoutId, redPackageGroupbyOrderNumberList);
        mactivity = activity;
        mContext = context;
        mState = state;
        int screenWidth = DensityUtil.screenWith(mactivity);
        recycler_width = screenWidth - DensityUtil.dip2px(mContext, 20);
        width = (screenWidth - DensityUtil.dip2px(mContext, 80)) / 3;
        height = width * 108 / 105;
    }

    @Override
    protected void convert(ViewHolder holder, final RedPackageGroupbyOrderNumber redPackageGroupbyOrderNumber, final int position) {
        mredPackageGroupbyOrderNumber = redPackageGroupbyOrderNumber;
        RecyclerView recyclerView = holder.getView(R.id.recyclerView);
        TextView order_number = holder.getView(R.id.order_number);
        TextView time = holder.getView(R.id.time);
        TextView consumer_name = holder.getView(R.id.consumer_name);
        TextView total = holder.getView(R.id.total);
        TextView upload = holder.getView(R.id.upload);
        ImageView state_image = holder.getView(R.id.state_image);
        if (redPackageGroupbyOrderNumber.getOrderNumber() != null) {
            order_number.setText(redPackageGroupbyOrderNumber.getOrderNumber());
        }
        if (redPackageGroupbyOrderNumber.getOrderTime() != null) {
            time.setText(DateUtil.fomatforDate(redPackageGroupbyOrderNumber.getOrderTime(), DateUtil.DATE_FORMATTER_1));
        }
        if (redPackageGroupbyOrderNumber.getConsumerName() != null) {
            consumer_name.setText(redPackageGroupbyOrderNumber.getConsumerName());
        }
        if (redPackageGroupbyOrderNumber.getOrderPrice() != null) {
            total.setText(BigDecimalUtils.forMate(redPackageGroupbyOrderNumber.getOrderPrice()).toString());
        }

        //横向RecyclerView
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mactivity, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        RedPackageDetailAdapter redPackageDetailAdapter = new RedPackageDetailAdapter(mactivity, mContext, R.layout.item_red_package_detail, mredPackageGroupbyOrderNumber.getDraws());
        recyclerView.setAdapter(redPackageDetailAdapter);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(recycler_width, height);
        layoutParams.setMargins(DensityUtil.dip2px(mContext, 10), 0, 0, 0);
        recyclerView.setLayoutParams(layoutParams);

        if (mState != null && mState.equals(GlobalConstant.RedPackageState.INITIAL)) {
            if (redPackageGroupbyOrderNumber.getOrderReceiptUrl() == null) {
                upload.setVisibility(View.VISIBLE);
                state_image.setVisibility(View.GONE);
            } else {
                upload.setVisibility(View.GONE);
                state_image.setVisibility(View.VISIBLE);
                state_image.setBackgroundResource(R.mipmap.red_package_unclaimed);
            }
        } else {
            upload.setVisibility(View.GONE);
            state_image.setVisibility(View.VISIBLE);
            if (mState.equals(GlobalConstant.RedPackageState.REFUSE)) {
                state_image.setBackgroundResource(R.mipmap.red_package_refuse);
            } else {
                state_image.setBackgroundResource(R.mipmap.red_package_used);
            }
        }
        state_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mState.equals(GlobalConstant.RedPackageState.REFUSE)) {
                    if (redPackageGroupbyOrderNumber.getDraws().size() == 0) {
                        return;
                    }
                    if (redPackageGroupbyOrderNumber.getDraws().get(0).getRemark() == null) {
                        return;
                    }
                    showLotteryResult(redPackageGroupbyOrderNumber.getDraws().get(0).getRemark());
                }
            }
        });

        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentStart.getInstance().startTakeAndSelectPhotoActivity(mactivity, GlobalConstant.FragmentType.INITIALFRAGMENT, position);
            }
        });
    }

    //展示弹框信息
    private void showLotteryResult(String remark) {
        View layout = LayoutInflater.from(mactivity).inflate(R.layout.remark_dialog, null);
        TextView content = (TextView) layout.findViewById(R.id.content);
        TextView confirm = (TextView) layout.findViewById(R.id.confirm);
        AlertDialog.Builder builder = new AlertDialog.Builder(mactivity);
        builder.setView(layout);
        builder.setCancelable(false);
        final AlertDialog alertDialog = builder.create();
        alertDialog.show();
        content.setText("拒绝原因: " + remark);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //刷新我的抽奖界面
                alertDialog.dismiss();
            }
        });
    }
}